package studentmanager;

import gui.GUIMain;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.sql.SQLException;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import structure.Student;

public class GUIStudentCell extends JPanel{
	private static final long serialVersionUID = 1L;
	private Student student;
	private JLabel mssv;
	private JLabel firstName;
	private JLabel lastName;
	private JLabel date;
	private JLabel idNumber;
	private JLabel mark;
	private JLabel user;
	private JLabel pass;
	
	private boolean isSelected;
	
	public GUIStudentCell() {
		setLayout(new GridBagLayout());
		setBorder(new EmptyBorder(10, 10, 10, 10));
		setOpaque(false);
		mssv = new JLabel();
		firstName = new JLabel();
		lastName = new JLabel();
		date = new JLabel();
		idNumber = new JLabel();
		mark = new JLabel();
		user = new JLabel();
		pass = new JLabel();
		
		GridBagConstraints c = new  GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		
		c.gridx=0;
		c.gridy=1;
		c.weightx = 0.2f;
		add(new JLabel("Họ tên :"),c);
		c.gridx=1;
		c.weightx = 0.2f;
		add(firstName,c);
		c.gridx=2;
		c.weightx = 1f;
		add(lastName,c);
		
		c.gridx=0;
		c.gridy=0;
		c.weightx = 0f;
		add(new JLabel("MSTS :"),c);
		c.gridx=1;
		c.gridwidth = 2;
		add(mssv,c);
		
		c.gridx=0;
		c.gridy=2;
		c.weightx = 0f;
		add(new JLabel("Ngày sinh :"),c);
		c.gridx=1;
		c.gridwidth = 2;
		add(date,c);
		
		c.gridx=0;
		c.gridy=3;
		c.gridwidth = 1;
		add(new JLabel("CMND :"),c);
		c.gridx=1;
		c.gridwidth = 2;
		add(idNumber,c);
		
		c.gridx=0;
		c.gridy=4;
		c.gridwidth = 1;
		add(new JLabel("Kết quả :"),c);
		c.gridx=1;
		c.gridwidth = 2;
		add(mark,c);
		
		c.gridx=0;
		c.gridy=5;
		c.gridwidth = 1;
		add(new JLabel("Tên Đ.nhập :"),c);
		c.gridx=1;
		c.gridwidth = 2;
		add(user,c);
		
		c.gridx=0;
		c.gridy=6;
		c.gridwidth = 1;
		add(new JLabel("Mật khẩu :"),c);
		c.gridx=1;
		c.gridwidth = 2;
		add(pass,c);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		g.setColor(new Color(255,255,255,0));
		if(isSelected){
			g.setColor(new Color(0, 0, 0,10));
		}
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		super.paintComponent(g);
	}
	
	public void setStudent(Student stu){
		this.student = stu;
		updateGUI();
	}
	
	private void updateGUI(){
		this.mssv.setText(this.student.getMSTS());
		this.firstName.setText(this.student.getFirstName());
		this.lastName.setText(this.student.getLastName());
		this.date.setText(this.student.getDateOfBirth().toString());
		this.idNumber.setText(this.student.getCMND());
		
		double m;
		try {
			m = GUIMain.getDb().getMarkofStudent(this.student.getiD());
		} catch (SQLException e) {
			m = -1;
		}
		
		if(m == -1){
			this.mark.setText("Chua thi");
		}else{
			this.mark.setText(String.valueOf(m));
		}

		this.user.setText(this.student.getUsername());
		this.pass.setText(this.student.getPassword());
	}

	public boolean isSelected() {
		return isSelected;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}
	
	public Student getStudent(){
		return this.student;
	}
	
}
